Skip to content

fix(hub): terminate child process trees on POSIX - #515

Open
webfansplz wants to merge 2 commits into
devframes:mainfrom
webfansplz:fix/hub-terminate-process-tree
Open

webfansplz wants to merge 2 commits into
devframes:mainfrom
webfansplz:fix/hub-terminate-process-tree

Conversation

@webfansplz

@webfansplz webfansplz commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Problem

On POSIX, terminate(), restart(), and stream cancel for a startChildProcess() session only send SIGTERM to the direct child. Task runners such as vp run exit on SIGTERM but do not forward it. The real process keeps running, keeps its port, and keeps the output pipe open. Because the pipe stays open, close never fires, getResult() never resolves, and the session stays running. The Terminals "Kill" button has the same problem.

Fix

killProcessTree() now reads the process table with ps -A -o pid=,ppid=, collects all descendants, and sends SIGTERM to them before the root. This matches the Windows taskkill /T path. If ps fails, it falls back to cp.kill(). Spawn options and signal handling do not change, so Ctrl+C in the host terminal works as before.

🤖 Generated with Claude Code

Copilot AI balanced review requested due to automatic review settings December 1, 2026 04:11
@vercel

vercel Bot commented Dec 1, 2026

Copy link
Copy Markdown

@webfansplz is attempting to deploy a commit to the NuxtLabs Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

👁️‍🗨️ Review this pull request with grouped, summarized diffs at:
👉 https://pulls.review/gh/devframes/devframe/515?from=github-actions[bot]

raw result

💭 analyzed by vercel-ai-gateway/anthropic/claude-sonnet-5
🕰️ 2026-12-01 04:29 UTC
🔗 head ba65678
🤖 automated by pulls.review

{
  "headSha": "ba65678a93c30eff3e5f34d76b52c90cbb81fdbb",
  "result": {
    "overallSummary": "Fixes a bug where `terminate()`/`restart()`/stream-cancel on POSIX only killed the direct child process, leaving orphaned grandchildren (e.g. task runners that don't forward SIGTERM) running and holding ports/pipes open, which prevented `close` and `getResult()` from ever resolving. `killProcessTree()` now enumerates the full process tree via `ps` and signals it bottom-up, mirroring the existing Windows `taskkill /T` path; a related fix ensures `restart()` doesn't prematurely close the new run's stream while waiting for the old tree to die.",
    "groups": [
      {
        "key": "core-fix",
        "label": "POSIX tree kill fix",
        "summary": "Makes `killProcessTree()` walk the process table on POSIX (via `ps -A -o pid=,ppid=`) and SIGTERM every descendant before the root, matching the existing Windows `taskkill /T` behavior, so task runners that don't forward SIGTERM no longer leave orphaned children holding ports/pipes open. Also fixes `restart()` so the old run's stream/listeners aren't torn down prematurely while the old tree is being killed, by bumping `runId` before awaiting `killRun` and letting `settle`/data handlers use `currentRun` correctly instead of bailing out based on a stale check.",
        "category": "core",
        "filePaths": [
          "packages/hub/src/node/host-terminals.ts"
        ],
        "critical": true,
        "lineNotes": [
          {
            "path": "packages/hub/src/node/host-terminals.ts",
            "side": "additions",
            "line": 58,
            "text": "Children are terminated before their parents (reversed tree order) so a parent can't respawn/reparent a child before it's killed."
          },
          {
            "path": "packages/hub/src/node/host-terminals.ts",
            "side": "additions",
            "line": 65,
            "text": "Falls back silently to `cp.kill()` if `ps` fails (e.g. not available), matching prior behavior for the direct child only."
          }
        ]
      },
      {
        "key": "tests",
        "label": "Terminal lifecycle tests",
        "summary": "Adds regression tests: one spawns a nested child process and asserts `terminate()` kills the whole tree, and one verifies that during `restart()` the old run's stream keeps flowing (and resolves without `exitCode`) while the new run's output is still delivered to the sink.",
        "category": "tests",
        "filePaths": [
          "packages/hub/src/node/__tests__/host-terminals.test.ts"
        ],
        "fileNotes": [
          {
            "path": "packages/hub/src/node/__tests__/host-terminals.test.ts",
            "text": "Mocks `tinyexec`'s `exec` to intercept the `ps` snapshot call and deliberately delay it until the old process closes, to exercise the race condition fixed in `restart()`."
          }
        ]
      }
    ],
    "schemaVersion": 1,
    "source": "llm",
    "generatedAt": "2026-12-01T04:29:56.683Z",
    "model": "vercel-ai-gateway/anthropic/claude-sonnet-5",
    "locale": "en"
  }
}

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical restart race allows the outgoing command to permanently close the shared stream during process snapshotting, discarding replacement output and causing subsequent restarts to fail.

1 open issue
What changed in this PR

This PR updates the hub’s child-process termination on POSIX to stop descendants that would otherwise keep ports and output pipes open.

Changes:

  • Uses a ps snapshot to find descendants and send SIGTERM before signaling the root process.
  • Preserves Windows tree termination and falls back to cp.kill() if process discovery fails.
  • Adds a POSIX regression test for terminating a grandchild and resolving the session result.
File Description
packages/​hub/​src/​node/​host-terminals.ts Adds process-tree discovery and descendant-first termination on POSIX.
packages/​hub/​src/​node/​__tests__/​host-terminals.test.ts Tests grandchild termination and the result’s killed status.

Review effort: Balanced

Comment thread packages/hub/src/node/host-terminals.ts Resolved
Copilot AI balanced review requested due to automatic review settings December 1, 2026 04:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Restart can spawn a replacement before the old process tree exits, risking EADDRINUSE; bounded shutdown waiting and a regression test with a delayed-shutdown descendant are needed.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Wait for old process tree to exit before restarting

packages/​hub/​src/​node/​host-terminals.ts:75

process.kill() sends SIGTERM but does not wait for the processes to exit. Returning here lets restart() spawn the replacement while an old descendant is still doing asynchronous cleanup and holding its listening port, which can cause EADDRINUSE. Wait for the old tree to stop using a bounded shutdown policy, and add a restart test with a descendant that delays shutdown and reuses the same port.

1 resolved since last review

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants